
package estore;

import estore.Product3;

import java.io.*;
import java.net.Socket;
import java.util.List;
import java.util.Scanner;

public class EStoreClient {
    private static final String SERVER_HOST = "localhost";
    private static final int SERVER_PORT = 8888;
    private Scanner scanner = new Scanner(System.in);

    public void start() {
        while (true) {
            showMenu();
            int choice = getIntInput("请选择操作: ");
            handleChoice(choice);
        }
    }

    private void showMenu() {
        System.out.println("\n=== 商品管理系统 (客户端) ===");
        System.out.println("1. 查看所有商品");
        System.out.println("2. 查看商品详情");
        System.out.println("3. 添加商品");
        System.out.println("0. 退出");
    }

    private void handleChoice(int choice) {
        switch (choice) {
            case 1:
                browseProducts();
                break;
            case 2:
                viewProduct();
                break;
            case 3:
                addProduct();
                break;
            case 0:
                exitProgram();
                break;
            default:
                System.out.println("无效选择！");
        }
    }

    private void browseProducts() {
        Socket socket = null;
        try {
            // 创建Socket连接服务器
            socket = new Socket(SERVER_HOST, SERVER_PORT);

            // 获取输入输出流
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

            // 发送请求
            oos.writeObject("GET_ALL_PRODUCTS");
            oos.flush();

            // 接收响应
            Object response = ois.readObject();

            if (response instanceof List) {
                @SuppressWarnings("unchecked")
                List<Product3> products = (List<Product3>) response;

                if (products.isEmpty()) {
                    System.out.println("暂无商品！");
                } else {
                    System.out.println("所有商品:");
                    for (Product3 product : products) {
                        System.out.println(product);
                    }
                }
            } else {
                System.out.println("服务器响应异常: " + response);
            }

        } catch (IOException | ClassNotFoundException e) {
            System.err.println("连接服务器失败: " + e.getMessage());
            System.out.println("请确保服务器已启动！");
        } finally {
            // 确保Socket被关闭
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    System.err.println("关闭连接时出错: " + e.getMessage());
                }
            }
        }
    }

    private void viewProduct() {
        System.out.print("输入商品ID: ");
        String id = scanner.nextLine();
        // 这里需要实现通过Socket获取单个商品详情的逻辑
        System.out.println("查看商品详情功能待实现...");
    }

    private void addProduct() {
        System.out.print("输入商品ID: ");
        String id = scanner.nextLine();
        System.out.print("输入商品名称: ");
        String name = scanner.nextLine();
        double price = getDoubleInput("输入商品价格: ");
        int stock = getIntInput("输入商品库存: ");

        // 这里需要实现通过Socket添加商品的逻辑
        System.out.println("添加商品功能待实现...");
    }

    private void exitProgram() {
        System.out.println("感谢使用，再见！");
        System.exit(0);
    }

    private int getIntInput(String prompt) {
        System.out.print(prompt);
        while (!scanner.hasNextInt()) {
            System.out.print("请输入有效数字: ");
            scanner.next();
        }
        int result = scanner.nextInt();
        scanner.nextLine();
        return result;
    }

    private double getDoubleInput(String prompt) {
        System.out.print(prompt);
        while (!scanner.hasNextDouble()) {
            System.out.print("请输入有效价格: ");
            scanner.next();
        }
        double result = scanner.nextDouble();
        scanner.nextLine();
        return result;
    }

    public static void main(String[] args) {
        new EStoreClient().start();
    }
}
